package com.atguigu.controller;

import com.atguigu.client.CartFeignClient;
import com.atguigu.client.OrderFeignClient;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.SkuInfo;
import com.atguigu.result.RetVal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class WebCartInfoController {

    @Autowired
    ProductFeignClient productFeignClient;
    @Autowired
    SearchFeignClient searchFeignClient;
    @Autowired
    CartFeignClient cartFeignClient;
    @Autowired
    OrderFeignClient orderFeignClient;

    //实现跳转购物车页面http://cart.gmall.com/addCart.html?skuId=24&skuNum=1
    @RequestMapping("addCart.html")
    public String addCart(@RequestParam Long skuId, @RequestParam Integer skuNum, HttpServletRequest request) {
        //远程调用shop——cart把商品添加到数据库
        cartFeignClient.addToCart(skuId,skuNum);
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);
        return "cart/addCart";
    }

    //购物车列表显示
    @RequestMapping("cart.html")
    public String cartList(){
        return "cart/index";
    }

    //跳转结算页面
    @GetMapping("confirm.html")
    public String trade(Model model){
        RetVal<Map<String, Object>> retVal = orderFeignClient.confirm();
        model.addAllAttributes(retVal.getData());
        return "order/confirm";
    }
}


